package testcase;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

public class HttpClientDemo {
    private CookieStore store;
//    @Test
//    public void testName() throws IOException {
//        //Get不带参数方法
//        HttpGet get=new HttpGet("http://localhost:12306/demo");
//        //用来执行get方法的
//        DefaultHttpClient client=new DefaultHttpClient();
//        HttpResponse response = client.execute(get);
//        //将响应结果转为字符串
//        String result= EntityUtils.toString(response.getEntity());
//        System.out.println(result);
//    }
//
//    //带参数的get方法
//    @Test
//    public void testName2() throws  Exception{
//        HttpGet get=new HttpGet("http://localhost:12306/mockwithparam?name=chengliang&age=23");
//        //用来执行get方法的
//        DefaultHttpClient client=new DefaultHttpClient();
//        HttpResponse response = client.execute(get);
//        String result= EntityUtils.toString(response.getEntity());
//        System.out.println(result);
//
//
//    }
//
//    //get获取cookie
//    @Test
//    public void testNameGetCookie() throws Exception {
//        HttpGet get=new HttpGet("http://localhost:12306/mockGetgetcookie");
//        DefaultHttpClient client=new DefaultHttpClient();
//         HttpResponse response = client.execute(get);
//         //获取cookie信息
//        CookieStore store = client.getCookieStore();
//        List<Cookie> cookieList=store.getCookies();
//        for(Cookie cookie:cookieList){
//            String key=cookie.getName();
//            String value=cookie.getValue();
//            System.out.println("cookie key="+key+",cookie value="+value);
//        }
//        //将响应结果转为字符串
//        String result= EntityUtils.toString(response.getEntity());
//        System.out.println(result);
//    }

    //get获取cookie
    @Test
    public void testNameGetCookie2() throws Exception {

        HttpGet get=new HttpGet("http://localhost:12306/mockGetgetcookie");
        DefaultHttpClient client=new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        //获取cookie信息
       this.store = client.getCookieStore();
        List<Cookie> cookieList=store.getCookies();
        //遍历拿到键值对
        for(Cookie cookie:cookieList){
            String key=cookie.getName();
            String value=cookie.getValue();
            System.out.println("cookie key="+key+",cookie value="+value);
        }

    }

    //使用cookie访问Get请求
    @Test(dependsOnMethods = {"testNameGetCookie2"})//依赖于上面这个方法获取cookie
    public void testNamecookie3() throws IOException {
        //声明是get方法，后面跟请求地址
        HttpGet get=new HttpGet("http://localhost:12306/withCookiewithGet");
        DefaultHttpClient client=new DefaultHttpClient();

        client.setCookieStore(store);//获取cookie
        HttpResponse response = client.execute(get);
        //判断响应状态码是否正确
        int status= response.getStatusLine().getStatusCode();
        if(status==200){
            String result=EntityUtils.toString( response.getEntity());
            System.out.println(result);
        }

    }
}
